#!/bin/bash
PROJROOT=$PWD/../
DISTFILES=${PROJROOT}/distfiles
STAGE3=stage3-i686-20100622.tar.bz2
PORTAGE=portage-latest.tar.bz2
CHROOT=${PROJROOT}/chroot

# TODO needed utilities
# sudo, wget

if [ ! -e ${DISTFILES} ]; then
	mkdir ${DISTFILES}
fi
# donwload the latest stages
# download a stage 3 gentoo
if [ ! -e ${DISTFILES}/${STAGE3} ]; then
	wget -P ${DISTFILES} http://mirrors.kernel.org/gentoo/releases/x86/autobuilds/current-stage3/${STAGE3}
fi
# download the portage tree
if [ ! -e ${DISTFILES}/${PORTAGE} ]; then
	wget -P ${DISTFILES} http://mirrors.kernel.org/gentoo/releases/snapshots/current/${PORTAGE}
fi
# extract them
if [ ! -e ${CHROOT} ]; then
	mkdir ${CHROOT}
	sudo tar -xjpf ${DISTFILES}/${STAGE3} -C ${CHROOT}
	sudo tar -xjpf ${DISTFILES}/${PORTAGE} -C ${CHROOT}/usr
	sudo mkdir -p ${CHROOT}/opt/embtoo
	sudo mkdir -p ${CHROOT}/usr/local/portage
	echo "PORTDIR_OVERLAY=\"/opt/embtoo/overlay /usr/local/portage\"" >> ${CHROOT}/etc/make.conf
fi
